import numpy as np
from scipy.sparse import coo_matrix

def sprase_rand(m, n, p):
    '''
    Imports
    -------
    import numpy as np
    from scipy.sparse import coo_matrix
    
    Parameters
    ----------
    m : int
        Rows of generate sprase-matrix.
    n : int
        Columns of generate sprase-matrix.
    p : float
        Proportion of non-zero elements in sparse-matrix.

    Returns
    -------
    scipy.sparse.coo.coo_matrix
        Returns a random generated sprase-matrix(m x n) M,
        try print(M) to see it or use M.toarray() for trans 
        M to an array. (Gaussian distribution)
        
    Version: 1.0 writen by z.q.feng @2022.03.13
    '''
    if type(m) != int or type(n) != int:
        raise TypeError('Rows(m) and Columns(n) must be an interger!')
    if p <= 0 or p > 1:
        raise ValueError('p must in (0, 1] !')
    # Counts of non-zero elements in sparse-matrix
    count = int(m * n * p)
    # Indexs of non-zero elements in sparse-matrix
    rows = np.random.randint(0, m, count)
    cols = np.random.randint(0, n, count)
    # Values of non-zero elements in sparse-matrix
    # (Gaussian distribution)
    data = np.random.randn(len(rows))
    return coo_matrix((data, (rows, cols)), shape=(m, n)).toarray()